package com.qf.personal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.personal.entiy.*;
import com.qf.personal.mapper.*;
import com.qf.personal.service.ReportService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;


@Service
public class ReportServiceImpl implements ReportService {
    @Resource
    private ReportMapper reportMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private ThingsMapper thingsMapper;
    @Resource
    private LinkMapper linkMapper;
    @Resource
    private ManagementMapper managementMapper;
    @Resource
    private EmpMapper empMapper;
    @Resource
    private IndexMapper indexMapper;

    @Override
    @Transactional
    public RespResult<String> add(Report report) {
        Long id = IdWorker.getId();
        report.setId(id);
        int a = reportMapper.insert(report);
        try {
            if (!ObjectUtils.isEmpty(a)) {
                Things things1 = new Things();
                QueryWrapper<Type> QW = new QueryWrapper<>();
                QW.select("t_name").eq("t_id", report.getRTId());
                Type type = typeMapper.selectOne(QW);
                things1.setTName(type.getTName());

                QueryWrapper<Link> qw1 = new QueryWrapper<>();
                qw1.select("link_name").eq("link_id", report.getLId());
                Link link = linkMapper.selectOne(qw1);
                things1.setLName(link.getLinkName());

                QueryWrapper<Management> qw2 = new QueryWrapper<>();
                qw2.select("management_name").eq("management_id", report.getRMId());
                Management m = managementMapper.selectOne(qw2);
                things1.setMName(m.getManagementName());
                QueryWrapper<Emp> qw3 = new QueryWrapper<>();

                qw3.select("emp_name").eq("emp_id", report.getEId());
                Emp e = empMapper.selectOne(qw3);
                things1.setEName(e.getEmpName());
                things1.setEId(report.getEId());
                things1.setTime(new Date());
                things1.setId(id);
                int b = thingsMapper.insert(things1);
                if (!ObjectUtils.isEmpty(b)) {
                    return RespResult.success("申请成功");
                }
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return null;
    }

    @Override
    @Transactional
    public RespResult<String> deleteById(Long id) {
        int a = reportMapper.delete(id);
        try {
            if (a == 1) {
                int b = indexMapper.delete(id);
                if (b == 1) {
                    return RespResult.success("删除成功");
                }
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return null;
    }
}
